﻿using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
using WebAPI;
using System.Net.Sockets;
using System.Net;
using System.Windows.Navigation;
using Prism.DryIoc;
using Prism.Ioc;
using Prism.Modularity;
using Microsoft.Extensions.Configuration;
using System.Threading;
using Microsoft.Extensions.Hosting.Internal;
using WpfApp.ViewModels;
using WpfApp.Views;
using WpfApp.Common;
namespace WpfApp
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : PrismApplication
    {
        private static IHost webApiHost;
        protected override Window CreateShell()
        {
            return Container.Resolve<MainView>();
        }
        public App()
        {
            var iHostBuilder = Host.CreateDefaultBuilder();
            iHostBuilder.UseConsoleLifetime();
            iHostBuilder.ConfigureWebHostDefaults(webBuilder => webBuilder.UseStartup<Startup>().ConfigureKestrel((c, o) =>
            {
                var ip = IPAddress.Parse(c.Configuration["WebHost"].ToString());
                int port = Convert.ToInt32(c.Configuration["WebPort"].ToString());
                o.Listen(ip, port);
            }));//anchor升级web服务
            webApiHost = iHostBuilder.Build();
        }
        protected override void OnInitialized()
        {
            var service = App.Current.MainWindow.DataContext as IConfigureService;
            if (service != null)
                service.Configure();
            base.OnInitialized();
        }
        protected override void OnStartup(StartupEventArgs e)
        {
            webApiHost.StartAsync();
            base.OnStartup(e);
        }


        public static void ReStartWebApi()
        {
            webApiHost.StopAsync(TimeSpan.FromSeconds(1));
            webApiHost.Dispose();
            webApiHost.StartAsync();
        }

        protected override void OnExit(ExitEventArgs e)
        {
            webApiHost.StopAsync(TimeSpan.FromSeconds(1));
            webApiHost.Dispose();
            base.OnExit(e);
            Application.Current.Shutdown();
            Environment.Exit(0);
        }
        protected override void RegisterTypes(IContainerRegistry containerRegistry)
        {
            containerRegistry.Register<IUdpService, AnchorUdpService>();
            containerRegistry.Register<IDialogHostService, DialogHostService>();
            containerRegistry.Register<UdpAnchorViewModel>();
            containerRegistry.Register<SerialPortAnchorViewModel>();
            containerRegistry.RegisterForNavigation<MsgView, MsgViewModel>();
            containerRegistry.RegisterForNavigation<IndexView>();
            containerRegistry.RegisterForNavigation<TagView, TagViewModel>();
            containerRegistry.RegisterForNavigation<AnchorView, AnchorViewModel>();
            containerRegistry.RegisterForNavigation<SettingsView, SettingsViewModel>();
        }
    }
}
